package org.javaymw.springboot.config;

import org.activiti.JsonpCallbackFilter;
import org.javaymw.springboot.filter.SLogFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.Filter;

/**
 * 描述：
 * <p>
 *
 * @author: 赵新国
 * @date: 2018/7/2 10:23
 */
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/*")
        .allowedOrigins("http://localhost:8080")
        .allowedMethods("*")
        .allowedHeaders("*")
        .allowCredentials(false)
        .maxAge(3600);
    }

//    @Override
//    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
//        configurer.defaultContentType(MediaType.APPLICATION_JSON).favorPathExtension(true);
//    }

    /*
     * Configure ContentNegotiatingViewResolver
     */
//    @Bean
//    public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager) {
//        ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
//        resolver.setContentNegotiationManager(manager);
//
//        // Define all possible view resolvers
//        List<ViewResolver> resolvers = new ArrayList<ViewResolver>();
//
//        resolvers.add(excelViewResolver());
//
//        resolver.setViewResolvers(resolvers);
//        return resolver;
//    }

    /*
     * Configure View viewResolver to provide XLS output using Apache POI library to
     * generate XLS output for an object content
     */
//    @Bean
//    public ViewResolver excelViewResolver() {
//        return new ExcelViewResolver();
//    }



    @Bean
    public Filter sLogFilter(){
        return new SLogFilter();//自定义的过滤器
    }

    @Bean
    public Filter jsonpCallbackFilter(){
        return new JsonpCallbackFilter();//自定义的过滤器
    }

    /**
     * 注册自定义Filter
     * @return
     */
    @Bean
    public FilterRegistrationBean indexFilterRegistration() {
        // 注册记录日志Filter
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(sLogFilter());
        registration.addUrlPatterns("/admin/*");
        return registration;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean2(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(jsonpCallbackFilter());
        filterRegistrationBean.addUrlPatterns("/service/*");
        filterRegistrationBean.setOrder(1);
        return filterRegistrationBean;
    }
}
